package com.bnz.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.bnz.exception.MyException;
import com.bnz.pojo.User;
import com.bnz.service.UserService;
import com.bnz.util.R;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
@Controller
@RequestMapping("user")
public class UserController {
	@Reference
	private UserService userService;
	//1. 用户登录
	@RequestMapping("login1")
	public String login(String username, String password, HttpSession session){
		//1.1 规定用户名或admin，密码是123，就可以登录
		if("admin".equalsIgnoreCase(username) && "123".equalsIgnoreCase(password)){
			session.setAttribute("username",username);
			return "redirect:/student/search.do";
		}else{  //1.2 如果输入的用户名或密码有误，就直接到登录页面
			return "redirect:/login.html";
		}
	}

	//2. 全局异常处理--->特殊自定义异常
	@RequestMapping("test")
	@ResponseBody
	public void processException(){
		int b = 0;
		if(b == 0){
			throw new MyException("对不起，出现了被零除异常！");
		}
	}

	//3. 全局异常处理--->一般异常
	@RequestMapping("hello")
	@ResponseBody
	public void hello(){
		throw new RuntimeException();
	}

	//4. 进行文件上传 (同步表单上传)
	@PostMapping("upload")
	public String upload(HttpServletRequest request, MultipartFile mfile) throws IOException {
		//4.1 得到上传的文件的真实名称
		String originalFilename = mfile.getOriginalFilename();
		//4.2 取得文件后缀名
		String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
		//4.3 使用uuid生成主文件名
		String fileName = UUID.randomUUID().toString();
		//4.4 进行文件上传
		//4.4.1 得到/upload的真实的物理路径
		//① 得到application对象
		ServletContext context = request.getServletContext();
		//② 得到web根目录下的upload的真实的物理路径
		String realPath = context.getRealPath("/upload");
		//4.4.2 构造要上传的文件并进行上传
		File dest = new File(realPath + "/" + fileName + suffix);
		//4.4.3 开始文件上传
		mfile.transferTo(dest);
		//4.5 返回逻辑视图
		return "/upload/list";
	}
	//sync_upload
	//4. 进行文件上传
	@RequestMapping("sync_upload")
	@ResponseBody
	public R sync_upload(HttpServletRequest request, MultipartFile file) throws IOException {
		//4.1 得到上传的文件的真实名称
		String originalFilename = file.getOriginalFilename();
		//4.2 取得文件后缀名
		String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
		//4.3 使用uuid生成主文件名
		String fileName = UUID.randomUUID().toString();
		//4.4 进行文件上传
		//4.4.1 得到/upload的真实的物理路径
		//① 得到application对象
		ServletContext context = request.getServletContext();
		//② 得到web根目录下的upload的真实的物理路径
		String realPath = context.getRealPath("/upload");
		//4.4.2 构造要上传的文件并进行上传
		File dest = new File(realPath + "/" + fileName + suffix);
		//4.4.3 开始文件上传
		file.transferTo(dest);
		//4.4.4 定义图片的url地址
		String url = "http://localhost:9000/upload/" + dest.getName();
		//4.5 返回
		return R.ok().data("url",url);
	}

	//1. 用户登录
	@RequestMapping("/login")
	@ResponseBody
	public R login(){
		return R.ok().data("token",1);
	}

	//2. 用户信息
	@RequestMapping("/info")
	@ResponseBody
	public R info(){
		R r = R.ok().data("name","admin")
				.data("avatar","https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
		return r;
	}

	//3. 查询所有用户
	@GetMapping("findAll")
	@ResponseBody
	public R findAll(){
		List<User> userList = userService.findAll();
		return R.ok().data("list",userList);
	}

	//4. 添加用户
	@PostMapping("add")
	@ResponseBody
	public R add(@RequestBody User user){
		//4.1 添加用户
		userService.add(user);
		//4.2 返回
		return R.ok().message("添加用户成功！");
	}

}
